﻿using SolidWorks.Interop.sldworks;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;

namespace SolidworksDLL
{
    public partial class TestFrm2 : Form
    {
        public TestFrm2()
        {
            InitializeComponent();
        }

        private void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (CheckSW() == false)
                {
                    return;
                }
                ModelDoc2 swModel = SolidWorksHelp.swApp.ActiveDoc;
                if (swModel.GetType() != 1)
                {
                    return;
                }
                DateTime dateTime = DateTime.Now;

                SelectionMgr swSelectionManager = (SelectionMgr)swModel.SelectionManager;
                int n = swSelectionManager.GetSelectedObjectCount2(-1);
                if (n == 0)
                {
                    return;
                }

                object obj = swSelectionManager.GetSelectedObject6(1, -1);
                if (obj == null)
                {
                    return;
                }
                int type = swSelectionManager.GetSelectedObjectType3(1, -1);
                if (type != 22)
                {
                    return;
                }
                Feature feature = (Feature)obj;

                Begin(ref dateTime);

                treeView1.Nodes.Clear();

                TreeNode rootNode = treeView1.Nodes.Add(feature.Name);
                rootNode.Tag = new NodeData { Name = feature.Name, type = 0, obj = feature };

                //所有面
                List<Face2> faces = BodyHelp.GetAllFaces(feature);
                if (faces == null)
                {
                    End(dateTime);
                    return;
                }

                for (int i = 0; i < faces.Count; i++)
                {
                    richTextBox1.AppendText(System.Environment.NewLine + "面：" + i.ToString());
                    TreeNode faceNode = rootNode.Nodes.Add("面：" + i.ToString());
                    faceNode.Tag = new NodeData { Name = "面：" + i.ToString(), type = 1, obj = faces[i] };

                    List<Edge> edges = BodyHelp.GetAllEdges(faces[i]);
                    if (edges == null)
                    {
                        End(dateTime);
                        continue;
                    }
                    for (int j = 0; j < edges.Count; j++)
                    {
                        richTextBox1.AppendText(System.Environment.NewLine + "边：" + j.ToString());
                        TreeNode edgeNode = faceNode.Nodes.Add("边：" + j.ToString());
                        edgeNode.Tag = new NodeData { Name = "边：" + j.ToString(), type = 2, obj = edges[j] };

                        List<Vertex> verteices = BodyHelp.GetAllVertexs(edges[j]);
                        if (verteices == null)
                        {
                            End(dateTime);
                            continue;
                        }

                        for (int k = 0; k < verteices.Count; k++)
                        {
                            double[] point = verteices[k].GetPoint();

                            richTextBox1.AppendText(System.Environment.NewLine + "点：" + k.ToString());
                            TreeNode vertexNode = edgeNode.Nodes.Add("点：" + k.ToString());
                            vertexNode.Tag = new NodeData { Name = "点：" + k.ToString(), type = 3, obj = verteices[k] };
                        }
                    }
                }

                End(dateTime);
            }
            catch (Exception)
            {
                throw;
            }
        }

        public bool CheckSW()
        {
            bool b = SolidWorksHelp.GetSwAPP();
            if (b)
            {
                //MessageBox.Show("获取成功");
            }
            else
            {
                MessageBox.Show("获取失败");
                return b;
            }

            ModelDoc2 swModel = SolidWorksHelp.swApp.ActiveDoc;
            if (null == swModel)
            {
                MessageBox.Show("打开模型失败");
                return false;
            }

            return true;
        }

        public void Begin(ref DateTime dateTime)
        {
            dateTime = DateTime.Now;

            richTextBox1.AppendText(System.Environment.NewLine + "开始时间：" + dateTime.ToString("yyyy-MM-dd HH:mm:ss"));
        }

        public void End(DateTime dateTime)
        {
            DateTime dateTime1 = DateTime.Now;

            richTextBox1.AppendText(System.Environment.NewLine + "结束时间：" + dateTime1.ToString("yyyy-MM-dd HH:mm:ss"));
            richTextBox1.AppendText(System.Environment.NewLine + "总时间：" + (dateTime1 - dateTime).TotalSeconds.ToString());
        }

        private void TreeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            NodeData nodeData = (NodeData)e.Node.Tag;
            if (nodeData.type == 0)
            {
                Feature feature = (Feature)nodeData.obj;
                richTextBox1.AppendText(System.Environment.NewLine + "类型：特征" + ",名称：" + feature.Name + ",更新时间：" + feature.DateModified);
            }
            else if (nodeData.type == 1)
            {
                Face face = (Face)nodeData.obj;
                richTextBox1.AppendText(System.Environment.NewLine + "类型：面" + ",名称：" + nodeData.Name + ",面积：" + (face.GetArea() * 1000).ToString() + "平方厘米");
            }
            else if (nodeData.type == 2)
            {
                Edge edge = (Edge)nodeData.obj;
                richTextBox1.AppendText(System.Environment.NewLine + "类型：线" + ",名称：" + nodeData.Name + ",ID：" + edge.GetID().ToString());
            }
            else if (nodeData.type == 3)
            {
                Vertex vertex = (Vertex)nodeData.obj;
                double[] point = vertex.GetPoint();

                richTextBox1.AppendText(System.Environment.NewLine + "类型：点" + ",名称：" + nodeData.Name
                    + "X" + point[0].ToString() +
                     ",Y" + point[1].ToString() +
                      ",Z" + point[2].ToString());
            }
        }

        private void Button2_Click(object sender, EventArgs e)
        {
            try
            {
                if (CheckSW() == false)
                {
                    return;
                }
                ModelDoc2 swModel = SolidWorksHelp.swApp.ActiveDoc;
                if (swModel.GetType() != 3)
                {
                    return;
                }
                DateTime dateTime = DateTime.Now;

                Begin(ref dateTime);

                DrawingDoc drawingDoc = (DrawingDoc)swModel;
                string[] sheetNames = drawingDoc.GetSheetNames();
                foreach (var item in sheetNames)
                {
                    Sheet sheet = drawingDoc.Sheet[item];
                    richTextBox1.AppendText(System.Environment.NewLine + "Sheet名称：" + item);
                    List<SolidWorks.Interop.sldworks.View> views = DrawingsHelp.GetSheetViews(drawingDoc, item);

                    foreach (var item1 in views)
                    {
                        richTextBox1.AppendText(System.Environment.NewLine + "视图名称：" + item1.Name);
                        List<DisplayDimension> displayDimensions = DrawingsHelp.GetViewsDim(item1);
                        if (displayDimensions == null)
                        {
                            richTextBox1.AppendText(System.Environment.NewLine + "无尺寸名称");
                            continue;
                        }
                        foreach (var item2 in displayDimensions)
                        {
                            Dimension dimension = item2.GetDimension();

                            richTextBox1.AppendText(System.Environment.NewLine + "尺寸名称：" + dimension.Name + ",尺寸值：" + (dimension.SystemValue * 1000).ToString() + "mm");
                        }
                    }
                }

                End(dateTime);
            }
            catch (Exception)
            {
                throw;
            }
        }
    }

    public class NodeData
    {
        public string Name;

        /// <summary>
        /// feature 0;face 1;edge 2;vertex 3
        /// </summary>
        public int type;

        public object obj;
    }
}